package com.wxf.web;

import com.wxf.bean.Users;
import com.wxf.service.UsersService;
import com.wxf.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1-获取请求信息
        String userName = req.getParameter("TxtUserName");
        String password = req.getParameter("TxtPassword");
        // 2-服务器处理
        UsersService usersService = new UsersServiceImpl();
        Users user = usersService.login(userName,password);
        if(user!=null){
            req.getSession().setAttribute("user",user);
            resp.sendRedirect("/index.jsp");

        }else{
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter pw = resp.getWriter();
            pw.println("<script>location.href='login.jsp';alert('用户名或密码不正确')</script>");
        }
    }
}
